﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.Card;
using System.Threading;
using Yugi_Gui.Animation;
using Yugi_Gui;
namespace Yugi.Client.Control
{
    public partial class YTableControl : UserControl, IInvoke
    {
        #region BF

        void InitBattlePharse()
        {
            foreach (var item in model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan)
            {
                YCard temp = Data[item.YCard.ID].CardControl;
                if (temp.State == YTrangThaiLaBai.Attact_Up && item.YCard.CardType == YType.Monster)
                {
                    temp.MouseLeftButtonDown += new MouseButtonEventHandler(YTableControl_MouseLeftButtonDown_Nguoichoi);
                    // Cho Phep Thuc Hien Tan Cong
                    temp.DaTanCong = false;
                }
            }
            foreach (var item in model.YSupervisorModel.Doi_Thu.YBaiTrenBan)
            {
                YCard temp = Data[item.YCard.ID].CardControl;
                if (item.YCard.CardType == YType.Monster)
                {
                    temp.MouseLeftButtonDown += new MouseButtonEventHandler(temp_MouseLeftButtonDown_Doithu);
                }
            }
            // Tan Cong Direct
            tan_cong_direct = model.YSupervisorModel.Doi_Thu.BiTanCongTrucTiep();
            is_battle_phase = true;
            
        }
        void KiemTraLatBai(int id)
        {
            if (state_card_doi_thu == Common.YMSG.CARD_TRANGTHAI_NAM_UP)
            {
                YCard card = Data[id].CardControl;
                card.State = YTrangThaiLaBai.Defend_Up;

            }
        }
        void temp_MouseLeftButtonDown_Doithu(object sender, MouseButtonEventArgs e)
        {
           
                if (model.IsProcess)
                {
                    return;
                }
                YCard card = sender as YCard;
                if (card2 == null)
                {
                    card2 = ((CardModel)card.DataContext).YCard;
                    // Update State doi thu
                    if (card.State == YTrangThaiLaBai.Defend_Down)
                        state_card_doi_thu = Common.YMSG.CARD_TRANGTHAI_NAM_UP;
                    else if (card.State == YTrangThaiLaBai.Defend_Up)
                        state_card_doi_thu = Common.YMSG.CARD_TRANGTHAI_NAM_MO;
                    else if (card.State == YTrangThaiLaBai.Attact_Up)
                        state_card_doi_thu = Common.YMSG.CARD_TRANGTHAI_DUNG_MO;
                }
                else
                    card2 = null;
                if (card1 != null && card2 != null)
                {
                    // Lat La Bai Up Neu Co
                    KiemTraLatBai(card2.ID);
                    //Danh Dau Da Tan Cong Roi
                    Data[card1.ID].CardControl.DaTanCong = true;
                    
                    
                  

                

                    //SendMessage Tan Cong
                    this.model.YSupervisorModel.SendMessageToServer(Common.YMSG.CARD_TANCONG, card1.ID, card2.ID, state_card_doi_thu);
                    card1 = null;
                    card2 = null;


                   
                   

                 
                }
           
        }

        void YTableControl_MouseLeftButtonDown_Nguoichoi(object sender, MouseButtonEventArgs e)
        {
          
                if (model.IsProcess)
                {
                    MessageBox.Show("Dang Cho KQ");
                }
            
                YCard card = sender as YCard;
                // La Da Tan Cong Roi Khong Duoc Tan Cong Tiep
                if (card.DaTanCong)
                {
                    if (card.State == YTrangThaiLaBai.Select)
                    {
                        card.State = YTrangThaiLaBai.Attact_Up;
                       
                    }
                    return;
                }
                //Card chua thuc hien tan cong
                if (card.State == YTrangThaiLaBai.Select)
                {
                    card.State = YTrangThaiLaBai.Attact_Up;
                    card1 = null;
                }
                else
                {
                    card.State = YTrangThaiLaBai.Select;
                    card1 = ((CardModel)card.DataContext).YCard;
                }
               
          
        }
        void XoaBai(int id)
        {
            try
            {
                YCard card = Data[id].CardControl;
                //Xoa Bai Trong Mang Danh Dau
                int x = Grid.GetRow(Data[id].Host);
                int y = Grid.GetColumn(Data[id].Host);
                ViTri[x, y] = false;
                // Xu ly
                CardModel m = (CardModel)card.DataContext;
                if (this.model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan.Contains(m))
                {
                    this.model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan.Remove(m);
                    // Them vao mo nguoi choi
                    this.model.YSupervisorModel.Mo_Nguoi_Choi.Add(m);

                    
                }
                else
                {
                    this.model.YSupervisorModel.Doi_Thu.YBaiTrenBan.Remove(m);
                    // Them vao mo doi thu
                    this.model.YSupervisorModel.Mo_Doi_Thu.Add(m);

                    tan_cong_direct = this.model.YSupervisorModel.Doi_Thu.BiTanCongTrucTiep();
                    
                }
                card.State = YTrangThaiLaBai.Delete;
                
                ThreadPool.QueueUserWorkItem((o) =>
                {
                    Delay(1000);
                    Action ac = () =>{
                        try
                        {
                            this.container.Children.Remove(Data[id].Host);
                        }
                        catch {
                            MessageBox.Show("loi xoa bai: khong tim thay");
                        }

                        // Xoa Database
                        Data.Remove(id);
                      
                    };
                    this.Dispatcher.Invoke(ac);
                });
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(id.ToString() + " Khong Ton Tai");
            }

        }
        void DelBattlePharse()
        {
            foreach (var item in model.YSupervisorModel.Nguoi_Choi.YBaiTrenBan)
            {
                YCard temp = Data[item.YCard.ID].CardControl;
                if (temp.State == YTrangThaiLaBai.Select)
                {
                    temp.State = YTrangThaiLaBai.Attact_Up;
                    temp.MouseLeftButtonDown -= new MouseButtonEventHandler(YTableControl_MouseLeftButtonDown_Nguoichoi);
                }
                else
                    if (temp.State == YTrangThaiLaBai.Attact_Up && item.YCard.CardType == YType.Monster)
                    {
                        temp.MouseLeftButtonDown -= new MouseButtonEventHandler(YTableControl_MouseLeftButtonDown_Nguoichoi);
                    }
            }
            foreach (var item in model.YSupervisorModel.Doi_Thu.YBaiTrenBan)
            {
                YCard temp = Data[item.YCard.ID].CardControl;
                if (item.YCard.CardType == YType.Monster)
                {
                    temp.MouseLeftButtonDown -= new MouseButtonEventHandler(temp_MouseLeftButtonDown_Doithu);
                }
            }
            is_battle_phase = false;
        }
        private void Delay(int time)
        {
            AutoResetEvent evn = new AutoResetEvent(false);
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                Thread.Sleep(time);
                evn.Set();
            });
            evn.WaitOne();
        }

        #endregion

        #region Animation
        private void AnimationBiTanCong(int id1, int id2)
        {
            Point p1 = this.Data[id1].Position;
            Point p2 = this.Data[id2].Position;
            Linker.explosive.Show(); Linker.sw_fly.Show();
            Action p = () => { Linker.explosive.Hide(); Linker.sw_fly.Hide(); };
            Linker.sw_fly.From = p1;
            Linker.sw_fly.To = p2;
            Linker.explosive.Center = p2;
            Linker.BattleSchedue.SetFinal(p);
            Linker.BattleSchedue.Start();
            

        }
        private void AniamationBiTanCongDirect(int id,int type)
        {
            Point p1 = this.Data[id].Position;
            Point p2;
            if(type==0)
                // Bi Doi Thu Danh
                p2= new Point(500, 300);
            else
                //Nguoi Choi Danh Doi Thu
                p2 = new Point(500, 150);
            Linker.explosive.Show(); Linker.sw_fly.Show();
            Action p = () => { Linker.explosive.Hide(); Linker.sw_fly.Hide(); };
            Linker.sw_fly.From = p1;
            Linker.sw_fly.To = p2;
            Linker.explosive.Center = p2;
            Linker.BattleSchedue.SetFinal(p);
            Linker.BattleSchedue.Start();
        }
        #endregion

    }
}
